package tools

import (
	"math"
)

type PageUtil[T comparable] struct {
	TotalPage   int32 `json:"totalPage"`
	TotalCount  int32 `json:"totalCount"`
	CurrentPage int32 `json:"currentPage"`
	PageSize    int32 `json:"pageSize"`
	HasNextPage bool  `json:"hasNextPage"`
	Prev        int32 `json:"prev"`
	Next        int32 `json:"next"`
	Records     []T   `json:"records"`
}

func NewPageUtil[T comparable](currentPage, pageSize, totalNum int32, records []T) PageUtil[T] {
	totalPage := 0
	if totalNum%pageSize == 0 {
		totalPage = int(totalNum / pageSize)
	} else {
		totalPage = int(totalNum/pageSize) + 1
	}
	totalPage = int(math.Max(float64(totalPage), 1))
	nextPage := math.Min(float64(currentPage+1), float64(totalPage))
	return PageUtil[T]{
		CurrentPage: currentPage,
		PageSize:    pageSize,
		TotalCount:  totalNum,
		TotalPage:   int32(totalPage),
		Prev:        int32(math.Max(float64(currentPage-1), 1)),
		Next:        int32(nextPage),
		HasNextPage: nextPage != float64(totalPage),
		Records:     records,
	}
}
